package com.yilin.tms.truck.controller;

import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.annotation.Explain;
import com.yilin.tms.core.commons.annotation.NotNull;
import com.yilin.tms.core.commons.annotation.SkipLoginVerify;
import com.yilin.tms.core.commons.annotation.UserTarget;
import com.yilin.tms.core.entity.truck.TruckSource;
import com.yilin.tms.core.entity.user.UserType;
import com.yilin.tms.truck.service.ITruckSourceService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 车源控制器
 */
@RestController
@RequestMapping("/truck/source/")
public class TruckSourceController extends BaseController {
    @Resource
    ITruckSourceService sourceService;

    /**
     * 获取车源分页
     * 按距离排序时传入position即可，无需传入sort
     */
    @PostMapping(value = "getTruckSourcePage")
    @NotNull({"page", "limit"})
    public ReturnData<PageData<TruckSource>> getTruckSourcePage(TruckSource query, @Explain("页码") Integer page, @Explain("条数") Integer limit) {
        PageData<TruckSource> pageData = sourceService.getTruckSourcePage(getLoginUser(), query, page, limit);
        return jsonView(true, "获取成功", pageData);
    }

    /**
     * 获取车源详情
     */
    @PostMapping(value = "getTruckSourceDetail")
    @NotNull("vehicleId")
    public ReturnData<TruckSource> getTruckSourceDetail(@Explain("车辆ID") String truckSourceId) {
        TruckSource truckSource = sourceService.getTruckSourceDetail(getLoginUser(),truckSourceId);
        return jsonView(true, "获取成功", truckSource);
    }

    /**
     * 获取车源分页 隐藏敏感信息
     * 按距离排序时传入position即可，无需传入sort
     */
    @SkipLoginVerify
    @PostMapping(value = "getPublicTruckSourcePage")
    @NotNull({"page", "limit"})
    public ReturnData<PageData<TruckSource>> getPublicTruckSourcePage(TruckSource query, @Explain("页码") Integer page, @Explain("条数") Integer limit) {
        PageData<TruckSource> pageData = sourceService.getPublicTruckSourcePage(query, page, limit);
        return jsonView(true, "获取成功", pageData);
    }

    /**
     * 保存车源
     * */
    @PostMapping(value = "saveTruckSource")
    @UserTarget(UserType.truckerUser)
    public ReturnData<String> saveTruckSource(TruckSource param,String[] vehicleIds) {
        sourceService.saveTruckSource(getLoginUser(),param,vehicleIds);
        return jsonView(true, "保存车源成功");
    }

    /**
     * 上架（发布）车源
     * */
    @PostMapping(value = "releaseTruckSource")
    @NotNull("truckSourceId")
    public ReturnData<TruckSource> releaseTruckSource(String truckSourceId, Date endTime) {
        TruckSource truckSource = sourceService.getById(truckSourceId);
        truckSource.setReleaseTime(new Date());
        truckSource.setEndTime(endTime);
        truckSource.setSourceStatus(TruckSource.SourceStatus.release);
        sourceService.updateById(truckSource);
        return jsonView(true, "发布成功");
    }

    /**
     * 下架车源
     * */
    @PostMapping(value = "recallTruckSource")
    @NotNull("truckSourceId")
    public ReturnData<TruckSource> recallTruckSource(String truckSourceId) {
        TruckSource truckSource = sourceService.getById(truckSourceId);
        truckSource.setReleaseTime(null);
        truckSource.setSourceStatus(TruckSource.SourceStatus.save);
        sourceService.updateById(truckSource);
        return jsonView(true, "下架车源成功");
    }

    /**
     * 作废车源
     * */
    @PostMapping(value = "scrapTruckSource")
    @NotNull("truckSourceId")
    public ReturnData<TruckSource> scrapTruckSource(String truckSourceId) {
        TruckSource truckSource = sourceService.getById(truckSourceId);
        truckSource.setReleaseTime(null);
        truckSource.setSourceStatus(TruckSource.SourceStatus.scrap);
        sourceService.updateById(truckSource);
        return jsonView(true, "作废车源成功");
    }


}
